<template>
	<view class="">
		<view class="">
			<u-field v-model="mobile" label="手机号" prop="mobile" placeholder="请填写手机号"></u-field>
		</view>
		<u-field v-model="code" label="验证码" placeholder="请填写验证码">
			<u-button size="mini" slot="right" type="success" @click="getCode">{{codeText}}</u-button>
		</u-field>
		<u-verification-code ref="uCode" @change="codeChange"></u-verification-code>
		<view class="submit">
			<button type="default">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mobile: '',
				code: '',
				codeText: ''
			}
		},
		methods: {
			codeChange(text) {
				this.codeText = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 1000);
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss">
	.submit {
		margin-top: 80rpx;
		height: 175rpx;

		button {
			width: 75%;
			height: 100rpx;
			color: #FFFFFF;
			background: #5e3afe;
			border-radius: 10rpx;
		}
	}
</style>
